package com.itdev.matcherapp.android.gtalkclient;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.provider.Im;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.Menu.Item;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gtalkservice.IGTalkService;
import com.google.android.gtalkservice.IGTalkSession;
import com.itdev.matcherapp.android.R;

public class GTalkRequestList extends ListActivity {
	private IGTalkSession mGTalkSession;
	private ContentResolver contentResolver;
	private Cursor cursurRequested;
	private final String[] fromColumn = new String[]{Im.Subscriptions.USERNAME};
	private final int[] toView = new int[]{R.id.request_list_username};
	private SimpleCursorAdapter simpleCursorAdaptor;
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.request_list);
		contentResolver = getContentResolver();
		cursurRequested = contentResolver.query(Im.Subscriptions.CONTENT_URI, 
				null, null, null, null);
		cursurRequested.requery();
		simpleCursorAdaptor = new SimpleCursorAdapter(this, R.layout.request_list_item, cursurRequested, 
				fromColumn, toView);
		setListAdapter(simpleCursorAdaptor);
		LinearLayout linearLaout = (LinearLayout) simpleCursorAdaptor.getView(0, findViewById(R.id.request_list_checkbox), (ViewGroup) findViewById(R.id.request_list_linearlayout) );
		String text = (String) Boolean.toString(((CheckBox)linearLaout.getChildAt(1)).isChecked());
		
		bindService((new Intent()).setComponent(
                com.google.android.gtalkservice.GTalkServiceConstants.GTALK_SERVICE_COMPONENT),
                mConnection, 0);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, "Accept Request");
		return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		for(int i=0; i<simpleCursorAdaptor.getCount(); i++){
			LinearLayout linearLaout = (LinearLayout) simpleCursorAdaptor.getView(i, findViewById(R.id.request_list_checkbox), (ViewGroup) findViewById(R.id.request_list_linearlayout) );
			boolean isCheck = (((CheckBox)linearLaout.getChildAt(1)).isChecked());
			if(isCheck){
				String account = (String) ((TextView)linearLaout.getChildAt(0)).getText();
				try {
					mGTalkSession.approveSubscriptionRequest(account, null);
				} catch (DeadObjectException e) {
					Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
			}
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the GTalkService has been
            // established, giving us the service object we can use to
            // interact with the service. We are communicating with our
            // service through an IDL interface, so get a client-side
            // representation of that from the raw service object.
            IGTalkService GTalkService = IGTalkService.Stub.asInterface(service);

            try {
                mGTalkSession = GTalkService.getDefaultSession();
                
                if (mGTalkSession == null) {
                    // this should not happen.
                    return;
                }
            } catch (DeadObjectException ex) {
//                showMessage("Session not found");
            }

        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mGTalkSession = null;
//            mSendButton.setEnabled(false);
        }
    };
	@Override
	protected void onDestroy() {
		super.onDestroy();
        unbindService(mConnection);
	}
}
